package com.example.a12502.aihome;

import android.annotation.SuppressLint;
import android.os.*;
import android.os.Process;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.example.a12502.aihome.home.ListViewAdapter;
import com.example.a12502.aihome.home.Mysql;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.qmuiteam.qmui.widget.QMUIProgressBar;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static java.lang.Thread.sleep;

public class HomeFragment extends Fragment {

    ArrayList<HashMap<String,Object>> arrayList;
    ListViewAdapter listViewAdapter;

    Button button;
    QMUIProgressBar progressBar;
    ScrollView scrollView;
    TextView point;
    TextView conclusion;
    ListView listView;
    Handler handler;
    int flag;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        listView=view.findViewById(R.id.list_view);
        listViewAdapter=new ListViewAdapter(this.getContext(),getData());
        listView.setAdapter(listViewAdapter);
        listView.setFocusable(false);
        button=view.findViewById(R.id.test_start);
        progressBar=view.findViewById(R.id.progressBar);
        progressBar.setMaxValue(100);
        button.setOnClickListener(new ButtonClick());
        scrollView=view.findViewById(R.id.scrollView);
        point=view.findViewById(R.id.point);
        conclusion=view.findViewById(R.id.conclusion);
        point.setText("0");
        conclusion.setText("");
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);



        return view;
    }

    private ArrayList<HashMap<String, Object>> getData() {
        arrayList = new ArrayList<>();
        HashMap<String,Object> hashMap=new HashMap<>();
        HashMap<String,Object> hashMap1=new HashMap<>();
        HashMap<String,Object> hashMap2=new HashMap<>();
        hashMap.put("test_type","室内空气");
        hashMap.put("test_value","--");
        hashMap.put("test_time","-- --");
        hashMap.put("test_result","几乎无味，无健康影响，适宜居住");
        hashMap.put("picture",R.mipmap.bar_chart);

        hashMap1.put("test_type","PM2.5");
        hashMap1.put("test_value","--");
        hashMap1.put("test_time","-- --");
        hashMap1.put("test_result","轻度污染，请注意通风");
        hashMap1.put("picture",R.mipmap.bar_chart_1);

        hashMap2.put("test_type","温度");
        hashMap2.put("test_value","--");
        hashMap2.put("test_time","-- --");
        hashMap2.put("test_result","");
        hashMap2.put("picture",R.mipmap.bar_chart_2);

        arrayList.add(hashMap);
        arrayList.add(hashMap1);
        arrayList.add(hashMap2);

        return arrayList;
    }

    private class ButtonClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            flag=0;
            handler=new Handler();
            handler.post(runnable);
            handler.post(updateThread);


        }
    }

    private Runnable updateThread = new Runnable() {
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            Mysql mysql = new Mysql();
            Map<String, Object> temp = new HashMap<>();
            try {
                temp = mysql.query();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                mysql.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
//            System.out.print(temp.get("time") + "   ");
//            System.out.println(temp.get("state"));
            listViewAdapter.refresh(update(temp.get("time"),temp.get("state")));
            point.setText("98");
            conclusion.setText("空气质量良好");
            flag=1;
        }

    };

    private ArrayList<HashMap<String, Object>> update(Object time , Object state) {
        arrayList = new ArrayList<>();
        HashMap<String,Object> hashMap=new HashMap<>();
        HashMap<String,Object> hashMap1=new HashMap<>();
        HashMap<String,Object> hashMap2=new HashMap<>();
        hashMap.put("test_type","室内空气");
        hashMap.put("test_value",state);
        hashMap.put("test_time",time);
        hashMap.put("test_result","几乎无味，无健康影响，适宜居住");
        hashMap.put("picture",R.mipmap.bar_chart);



        hashMap1.put("test_type","PM2.5");
        hashMap1.put("test_value",Math.ceil(Math.random()*50)+"μg/m³");
        hashMap1.put("test_time",time);
        hashMap1.put("test_result","轻度污染，请注意通风");
        hashMap1.put("picture",R.mipmap.bar_chart_1);

        hashMap2.put("test_type","温度");
        hashMap2.put("test_value","18");
        hashMap2.put("test_time",time);
        hashMap2.put("test_result","");
        hashMap2.put("picture",R.mipmap.bar_chart_2);

        arrayList.add(hashMap);
        arrayList.add(hashMap1);
        arrayList.add(hashMap2);

        return arrayList;
    }

    Runnable runnable=new Runnable() {

        @Override
        public void run() {

            //如果进度小于100,则延迟1000毫秒之后重复执行runnable
            if(flag<1) {
                int prolength;
                prolength=progressBar.getProgress()+10;
                progressBar.setProgress(prolength);
                if (prolength < 100) {
                    handler.postDelayed(runnable, 100);
                }
                //否则，都置零，线程重新执行
                else {
                    progressBar.setProgress(0);
                    handler.post(runnable);
                }
            }else {
                progressBar.setProgress(100);
            }
        }
    };


}
